import FGUI_PGameMain from "src/FGUI/GameMain/FGUI_PGameMain";
import MesManager from "src/_T/Mes/MesManager";
import Desktopspeed from "./Desktopspeed";
import { E_AD_PlayerEvent } from "./PlayerEvent";

export class MarsJoy {
    /** 摇杆组件 */
    //private _JoyPlan: fairygui.GComponent;
    /** 点击事件组件*/
    private bgClickPlan: fairygui.GObject;
    /** 摇杆背景组件*/
    private joyBgRectTran: fairygui.GLoader;
    /**摇杆组件 */
    private joyRectTran: fairygui.GLoader;
    InitPosX: number;
    InitPosY: number;
    StarStageX: number;
    StarStageY: number;
    LastStageX: number;
    LastStageY: number;
    TopuchID: number = -1;
    IsTouch: boolean = false;
    Radius: number = 0;

    CilickWidth = 0;

    Init(ui: FGUI_PGameMain) {
        //获取摇杆组件
        //this._JoyPlan = ui.m_VirtualRocker.m_Joycomm;
        this.IsTouch = false;
        //获取事件触发组件
        this.bgClickPlan = ui.m_VirtualRocker.m_VirtualRocker;
        //摇杆中心背景
        this.joyBgRectTran = ui.m_VirtualRocker.m_ImageJoyBG;
        //摇杆
        this.joyRectTran = ui.m_VirtualRocker.m_ImageJoy;
        this.CilickWidth = ui.width / 2;
        // this.bgClickPlan.setXY(0, 0);
        // this.bgClickPlan.width = ui.width ;
        // this.bgClickPlan._height = ui._height ;
        this.InitPosX = this.joyBgRectTran.x + this.joyBgRectTran.width / 2;
        this.InitPosY = this.joyBgRectTran.y + this.joyBgRectTran.height / 2;
        this.TopuchID = -1;
        this.Radius = 75;
        this.bgClickPlan.on(Laya.Event.MOUSE_DOWN, this, this.OnClickDown);
        this.bgClickPlan.on(Laya.Event.MOUSE_MOVE, this, this.Drag);
        this.bgClickPlan.on(Laya.Event.MOUSE_UP, this, this.OnClickUp);
        this.HideJoy();
        Desktopspeed.Instance;
    }
    VectorZero = new Laya.Point(0, 0);
    private ShowJoy() {
        this.joyBgRectTran.visible = true;
        this.joyRectTran.visible = true;
    }
    private HideJoy() {
        this.joyBgRectTran.visible = false;
        this.joyRectTran.visible = false;
    }


    public OnClickDown(Point: Laya.Vector2) {
        if (Laya.stage.mouseX > this.CilickWidth) {
            return;
        }
        this.IsTouch = true;
        this.ShowJoy();
        Point = new Laya.Vector2(Laya.stage.mouseX, Laya.stage.mouseY);
        let _localPos: Laya.Point;
        _localPos = fairygui.GRoot.inst.globalToLocal(Point.x, Point.y);
        let Pos_X = _localPos.x;
        let Pos_Y = _localPos.y;
        this.StarStageX = Pos_X;
        this.StarStageY = Pos_Y;
        this.LastStageX = Pos_X;
        this.LastStageY = Pos_Y;
        this.joyBgRectTran.setXY(Pos_X - this.joyBgRectTran.width / 2, Pos_Y - this.joyBgRectTran.height / 2);
        this.joyRectTran.setXY(Pos_X - this.joyRectTran.width / 2, Pos_Y - this.joyRectTran.height / 2);
        let deltaX = Pos_X - this.InitPosX;
        let deltaY = Pos_Y - this.InitPosY;
        let Degrees = Math.atan2(deltaY, deltaX) * 180 / Math.PI;

    }
    public OnClickUp() {
        this.HideJoy();
        this.IsTouch = false;
        this.joyBgRectTran.setXY(this.InitPosX - this.joyBgRectTran.width / 2, this.InitPosY - this.joyBgRectTran.height / 2);
        this.joyRectTran.setXY(this.InitPosX - this.joyRectTran.width / 2, this.InitPosY - this.joyRectTran.height / 2);
        MesManager.event(E_AD_PlayerEvent.PlayerMove, [121]);
    }
    public Drag(Point: Laya.Vector2) {

        if (!this.IsTouch) return;
        Point = new Laya.Vector2(Laya.stage.mouseX, Laya.stage.mouseY);
        let _localPos: Laya.Point;
        _localPos = fairygui.GRoot.inst.globalToLocal(Point.x, Point.y);
        let Pos_X = _localPos.x;
        let Pos_Y = _localPos.y;
        let Move_X = Pos_X - this.LastStageX;
        let Move_Y = Pos_Y - this.LastStageY;
        this.LastStageX = Pos_X;
        this.LastStageY = Pos_Y;
        let Button_X = this.joyRectTran.x + Move_X;
        let Button_Y = this.joyRectTran.y + Move_Y;
        let deltaX = Button_X + this.joyRectTran.width / 2 - this.StarStageX;
        let deltaY = Button_Y + this.joyRectTran.height / 2 - this.StarStageY;
        let Red = Math.atan2(deltaY, deltaX);
        let Degrees = Red * 180 / Math.PI;
        let angle = Degrees;
        if (angle < 0) {
            angle += 360;
        }
        let upDir = Math.floor(angle / 3);
        MesManager.event(E_AD_PlayerEvent.PlayerMove, [upDir]);
        let MaxX = this.Radius * Math.cos(Red);
        let MaxY = this.Radius * Math.sin(Red);
        if (Math.abs(deltaX) > Math.abs(MaxX)) {
            deltaX = MaxX;
        }
        if (Math.abs(deltaY) > Math.abs(MaxY)) {
            deltaY = MaxY;
        }
        Button_X = this.StarStageX + deltaX;
        Button_Y = this.StarStageY + deltaY;
        //this.joyBgRectTran.setXY(Button_X- this.joyBgRectTran.width/2, Button_Y- this.joyBgRectTran.height/2);
        this.joyRectTran.setXY(Button_X - this.joyRectTran.width / 2, Button_Y - this.joyRectTran.height / 2);
    }

}